package com.matt.docpro.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.matt.docpro.config.Utils;
import com.matt.docpro.model.*;
import com.matt.docpro.service.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * DoctorController
 * Created by on 2024/02/07.
 */
@Controller
public class AdminController {

    @Autowired
    AppointmentService appointmentService;
    @Autowired
    ReviewService reviewService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    PatientService patientService;
    @Autowired
    CheckinService checkinService;
    @Autowired
    UserService userService;
    @Autowired
    AdminService adminService;
    @Autowired
    ClinicService clinicService;
    @Autowired
    BlogService blogService;
    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    FeedbackService feedbackService;
    @Autowired
    OutcomeService outcomeService;

    @RequestMapping("/admin-dashboard")
    public String init(Model model, HttpServletRequest request) throws Exception {
        List<User> users = userService.getAll();
        model.addAttribute("users", users);
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        if (!user.getRole().equals("0")) {
            return "redirect:/error403";
        }
        Admin admin = adminService.getById(user.getId());
        user.setName(admin.getName());
        model.addAttribute("admin", admin);
        model.addAttribute("user", user);
        int patientCount = patientService.getCount();
        int doctorCount = doctorService.getCount();
        int appointmentCount = appointmentService.getCount();
        model.addAttribute("patientCount", patientCount);
        model.addAttribute("doctorCount", doctorCount);
        model.addAttribute("appointmentCount", appointmentCount);
        List<Appointment> appointments = appointmentService.getAll();
        model.addAttribute("appointments", appointments);
        return "admin-dashboard";
    }

    @RequestMapping("/admin-profile")
    public String myProfile(Model model, HttpServletRequest request) throws Exception {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("user", user);
        model.addAttribute("admin", admin);
        return "admin-profile";
    }


    @PostMapping("/admin-profile/save")
    @ResponseBody
    public Map<String, Object> myProfile(Model model, Admin admin, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, admin.getId());
        if (filePath != null) {
            admin.setIcon(filePath);
        }
        adminService.update(admin);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/change-password-4")
    public String changePassword(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("user", user);
        model.addAttribute("admin", admin);
        return "change-password-4";
    }

    @RequestMapping("/admin-users")
    public String adminUsers(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<User> users = userService.getAll();
        model.addAttribute("users", users);
        model.addAttribute("user", user);
        return "admin-users";
    }

    @RequestMapping("/admin-users-detail")
    public String adminUsersDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        User user1 = new User();
        if (id != null) {
            user1 = userService.getById(id);
        } else {
            user1.setId(System.currentTimeMillis()+"");
        }
        model.addAttribute("userDetail", user1);
        model.addAttribute("user", user);
        return "admin-users-detail";
    }

    @PostMapping("/admin-users-detail/save")
    @ResponseBody
    public Map<String, Object> adminUsersDetailSave(Model model, User user, String isBanedStr, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        user.setBaned(isBanedStr.equals("1"));
        String filePath = Utils.uploadFile(iconFile, user.getId());
        if (filePath != null) {
            user.setIcon(filePath);
        }
        userService.update(user);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-users-detail/insert")
    @ResponseBody
    public Map<String, Object> adminUsersDetailInsert(Model model, User user, String isBanedStr, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        user.setBaned(isBanedStr.equals("1"));
        String filePath = Utils.uploadFile(iconFile,user.getId());
        if (filePath != null) {
            user.setIcon(filePath);
        }
        user.setPassword("123456");
        userService.insert(user);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-users-detail/delete")
    @ResponseBody
    public Map<String, Object> adminUsersDetailDelete(Model model, String id) {
        Map<String, Object> res = new HashMap<>();
        User user = userService.getById(id);
        userService.delete(user);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-patient")
    public String adminPatient(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Patient> patients = patientService.getAll();
        model.addAttribute("patients", patients);
        model.addAttribute("user", user);
        return "admin-patient";
    }

    @RequestMapping("/admin-patient-detail")
    public String adminPatientDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Patient patient = new Patient();
        if (id != null) {
            patient = patientService.getById(id);
        } else {
            patient.setId(System.currentTimeMillis()+"");
        }
        model.addAttribute("user", user);
        model.addAttribute("patient", patient);
        return "admin-patient-detail";
    }

    @PostMapping("/admin-patient-detail/save")
    @ResponseBody
    public Map<String, Object> adminPatientDetailSave(Model model, Patient patient, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, patient.getId());
        if (filePath != null) {
            patient.setIcon(filePath);
        }
        patientService.update(patient);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-patient-detail/insert")
    @ResponseBody
    public Map<String, Object> adminPatientDetailInsert(Model model, Patient patient, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        patient.setId(System.currentTimeMillis()+"");
        String filePath = Utils.uploadFile(iconFile, patient.getId());
        if (filePath != null) {
            patient.setIcon(filePath);
        }
        patientService.insert(patient);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-doc")
    public String adminDoc(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Doctor> doctors = doctorService.getAll();
        model.addAttribute("user", user);
        model.addAttribute("doctors", doctors);
        return "admin-doc";
    }

    @RequestMapping("/admin-doc-detail")
    public String adminDocDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Doctor doctor = new Doctor();
        if (id != null) {
            doctor = doctorService.getById(id);
        } else {
            doctor.setId(System.currentTimeMillis()+"");
        }
        model.addAttribute("user", user);
        model.addAttribute("doctor", doctor);
        return "admin-doc-detail";
    }

    @PostMapping("/admin-doc-detail/save")
    @ResponseBody
    public Map<String, Object> adminDocDetailSave(Model model, Doctor doctor, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, doctor.getId());
        if (filePath != null) {
            doctor.setIcon(filePath);
        }
        doctorService.update(doctor);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-doc-detail/insert")
    @ResponseBody
    public Map<String, Object> adminDocDetailInsert(Model model, Doctor doctor, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        doctor.setId(System.currentTimeMillis()+"");
        String filePath = Utils.uploadFile(iconFile, doctor.getId());
        if (filePath != null) {
            doctor.setIcon(filePath);
        }
        doctorService.insert(doctor);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-checkin")
    public String adminCheckin(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Checkin> checkins = checkinService.getAll();
        model.addAttribute("checkins", checkins);
        model.addAttribute("user", user);
        return "admin-checkin";
    }

    @RequestMapping("/admin-checkin-detail")
    public String adminCheckinDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Checkin checkin = new Checkin();
        if (id != null) {
            checkin = checkinService.getById(id);
        } else {
            checkin.setId(System.currentTimeMillis()+"");
        }
        model.addAttribute("checkin", checkin);
        model.addAttribute("user", user);
        return "admin-checkin-detail";
    }

    @PostMapping("/admin-checkin-detail/save")
    @ResponseBody
    public Map<String, Object> adminCheckinDetailSave(Model model, Checkin checkin, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, checkin.getId());
        if (filePath != null) {
            checkin.setIcon(filePath);
        }
        checkinService.update(checkin);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-checkin-detail/insert")
    @ResponseBody
    public Map<String, Object> adminCheckinDetailInsert(Model model, Checkin checkin, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        checkin.setId(System.currentTimeMillis()+"");
        String filePath = Utils.uploadFile(iconFile, checkin.getId());
        if (filePath != null) {
            checkin.setIcon(filePath);
        }
        checkinService.insert(checkin);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-appointment")
    public String adminAppointment(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Appointment> appointments = appointmentService.getAll();
        model.addAttribute("appointments", appointments);
        model.addAttribute("user", user);
        return "admin-appointment";
    }

    @RequestMapping("/admin-appointment-detail")
    public String adminAppointmentDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Appointment appointment = new Appointment();
        if (id != null) {
            appointment = appointmentService.getById(id);
        } else {
            appointment.setId("A"+System.currentTimeMillis());
        }
        List<Patient> patients = patientService.getAll();
        List<Doctor> doctors = doctorService.getAll();
        List<Clinic> clinics = clinicService.getAll();
        model.addAttribute("patients", patients);
        model.addAttribute("doctors", doctors);
        model.addAttribute("clinics", clinics);
        model.addAttribute("appointment", appointment);
        model.addAttribute("user", user);
        return "admin-appointment-detail";
    }

    @PostMapping("/admin-appointment-detail/save")
    @ResponseBody
    public Map<String, Object> adminAppointmentDetailSave(Model model, Appointment appointment) {
        Map<String, Object> res = new HashMap<>();
        appointmentService.update(appointment);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-appointment-detail/insert")
    @ResponseBody
    public Map<String, Object> adminAppointmentDetailInsert(Model model, Appointment appointment) {
        Map<String, Object> res = new HashMap<>();
        appointment.setId("A"+System.currentTimeMillis());
        appointmentService.insert(appointment);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-appointment-detail/delete")
    @ResponseBody
    public Map<String, Object> adminAppointmentDetailDelete(Model model, String id) {
        Map<String, Object> res = new HashMap<>();
        appointmentService.delete(id);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-clinic")
    public String adminClinic(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Clinic> clinics = clinicService.getAll();
        model.addAttribute("clinics", clinics);
        model.addAttribute("user", user);
        return "admin-clinic";
    }

    @RequestMapping("/admin-clinic-detail")
    public String adminClinicDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Clinic clinic = new Clinic();
        if (id != null) {
            clinic = clinicService.getById(id);
        } else {
            clinic.setId(""+System.currentTimeMillis());
        }
        model.addAttribute("clinic", clinic);
        model.addAttribute("user", user);
        return "admin-clinic-detail";
    }

    @PostMapping("/admin-clinic-detail/save")
    @ResponseBody
    public Map<String, Object> adminClinicDetailSave(Model model, Clinic clinic, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, clinic.getId());
        if (filePath != null) {
            clinic.setIcon(filePath);
        }
        clinicService.update(clinic);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-clinic-detail/insert")
    @ResponseBody
    public Map<String, Object> adminClinicDetailInsert(Model model, Clinic clinic, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        clinic.setId(""+System.currentTimeMillis());
        String filePath = Utils.uploadFile(iconFile, clinic.getId());
        if (filePath != null) {
            clinic.setIcon(filePath);
        }
        clinicService.insert(clinic);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-clinic-detail/delete")
    @ResponseBody
    public Map<String, Object> adminClinicDetailDelete(Model model, String id) {
        Map<String, Object> res = new HashMap<>();
        clinicService.delete(id);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-blog")
    public String adminBlog(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Blog> blogs = blogService.getAll();
        model.addAttribute("blogs", blogs);
        model.addAttribute("user", user);
        return "admin-blog";
    }

    @RequestMapping("/admin-blog-detail")
    public String adminBlogDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Blog blog = new Blog();
        if (id != null) {
            blog = blogService.getById(id);
        } else {
            blog.setId(""+System.currentTimeMillis());
        }
        model.addAttribute("blog", blog);
        model.addAttribute("user", user);
        return "admin-blog-detail";
    }

    @PostMapping("/admin-blog-detail/save")
    @ResponseBody
    public Map<String, Object> adminBlogDetailSave(Model model, Blog blog, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String filePath = Utils.uploadFile(iconFile, blog.getId());
        if (filePath != null) {
            blog.setImg(filePath);
        }
        blog.setCreateTime(DateUtil.date());
        blogService.update(blog);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-blog-detail/insert")
    @ResponseBody
    public Map<String, Object> adminBlogDetailInsert(Model model, HttpServletRequest request, Blog blog, MultipartFile iconFile) {
        Map<String, Object> res = new HashMap<>();
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        blog.setId(""+System.currentTimeMillis());
        blog.setCreatorId(user.getId());
        blog.setCreateTime(DateUtil.date());
        String filePath = Utils.uploadFile(iconFile, blog.getId());
        if (filePath != null) {
            blog.setImg(filePath);
        }
        blogService.insert(blog);
        res.put("state", "success");
        return res;
    }

    @PostMapping("/admin-blog-detail/delete")
    @ResponseBody
    public Map<String, Object> adminBlogDetailDelete(Model model, String id) {
        Map<String, Object> res = new HashMap<>();
        blogService.delete(id);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-feedback")
    public String adminFeedback(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        List<Feedback> feedbacks = feedbackService.getAll();
        model.addAttribute("feedbacks", feedbacks);
        model.addAttribute("user", user);
        return "admin-feedback";
    }

    @RequestMapping("/admin-feedback-detail")
    public String adminFeedbackDetail(Model model, HttpServletRequest request, String id) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Admin admin = adminService.getById(user.getId());
        model.addAttribute("admin", admin);
        Feedback feedback = feedbackService.getById(id);
        model.addAttribute("feedback", feedback);
        model.addAttribute("user", user);
        return "admin-feedback-detail";
    }

    @PostMapping("/admin-feedback-detail/delete")
    @ResponseBody
    public Map<String, Object> adminFeedbackDelete(Model model, String id) {
        Map<String, Object> res = new HashMap<>();
        feedbackService.deleteById(id);
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/outcome")
    public String outcome(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        List<Doctor> doctors = doctorService.getAll();
        Doctor doctor = doctors.get(0);
        // 日期数据，最近5天
        Map<Integer, List<String>> dateMap = new LinkedHashMap<>();
        DateTime today = DateUtil.date();
        String todayStr = DateUtil.format(today, "yyyy/MM/dd");
        String todayShowStr = DateUtil.format(today, "MM-dd")+" "+Utils.getWeekDay(today);
        List<String> dateStrList = new ArrayList<>();
        dateStrList.add(todayStr);
        dateStrList.add(todayShowStr);
        dateMap.put(1, dateStrList);
        String endDtStr = null;
        for (int i = 1; i < 5; i++) {
            DateTime nextDay = DateUtil.offsetDay(today, i);
            String nextDayStr = DateUtil.format(nextDay, "yyyy/MM/dd");
            String nextDayShowStr = DateUtil.format(nextDay, "MM-dd")+" "+Utils.getWeekDay(nextDay);
            List<String> nextDateStrList = new ArrayList<>();
            nextDateStrList.add(nextDayStr);
            nextDateStrList.add(nextDayShowStr);
            dateMap.put(i+1, nextDateStrList);
            endDtStr = nextDayStr;
        }
        model.addAttribute("dateMap", dateMap);

        List<Outcome> outcomeList0 = new ArrayList<>();
        List<Outcome> outcomeList1 = new ArrayList<>();
        List<Outcome> outcomeList2 = new ArrayList<>();
        List<Outcome> outcomeList3 = new ArrayList<>();
        List<Outcome> outcomeList0tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 0);
        List<Outcome> outcomeList1tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 1);
        List<Outcome> outcomeList2tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 2);
        List<Outcome> outcomeList3tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 3);
        for (int i = 1; i <= 5; i++) {
            String date = dateMap.get(i).get(0);
            outcomeList0.add(outcomeList0tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList1.add(outcomeList1tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList2.add(outcomeList2tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList3.add(outcomeList3tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
        }

        model.addAttribute("outcomeList0", outcomeList0);
        model.addAttribute("outcomeList1", outcomeList1);
        model.addAttribute("outcomeList2", outcomeList2);
        model.addAttribute("outcomeList3", outcomeList3);
        model.addAttribute("doctors", doctors);
        model.addAttribute("doctor", doctor);
        model.addAttribute("user", user);
        return "outcome";
    }

    @RequestMapping("/outcome/getData")
    public String outcomeGetData(Model model, String clinicId, String docId) {
        Doctor doctor = doctorService.getById(docId);
//        List<Doctor> doctors = doctorService.getAll();
        // 日期数据，最近5天
        Map<Integer, List<String>> dateMap = new LinkedHashMap<>();
        DateTime today = DateUtil.date();
        String todayStr = DateUtil.format(today, "yyyy/MM/dd");
        String todayShowStr = DateUtil.format(today, "MM-dd")+" "+Utils.getWeekDay(today);
        List<String> dateStrList = new ArrayList<>();
        dateStrList.add(todayStr);
        dateStrList.add(todayShowStr);
        dateMap.put(1, dateStrList);
        String endDtStr = null;
        for (int i = 1; i < 5; i++) {
            DateTime nextDay = DateUtil.offsetDay(today, i);
            String nextDayStr = DateUtil.format(nextDay, "yyyy/MM/dd");
            String nextDayShowStr = DateUtil.format(nextDay, "MM-dd")+" "+Utils.getWeekDay(nextDay);
            List<String> nextDateStrList = new ArrayList<>();
            nextDateStrList.add(nextDayStr);
            nextDateStrList.add(nextDayShowStr);
            dateMap.put(i+1, nextDateStrList);
            endDtStr = nextDayStr;
        }
        model.addAttribute("dateMap", dateMap);

        List<Outcome> outcomeList0 = new ArrayList<>();
        List<Outcome> outcomeList1 = new ArrayList<>();
        List<Outcome> outcomeList2 = new ArrayList<>();
        List<Outcome> outcomeList3 = new ArrayList<>();
        List<Outcome> outcomeList0tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 0);
        List<Outcome> outcomeList1tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 1);
        List<Outcome> outcomeList2tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 2);
        List<Outcome> outcomeList3tmp = outcomeService.getOutcomeByDateRangeClinicDocStage(todayStr, endDtStr, doctor.getClinicId(), doctor.getId(), 3);
        for (int i = 1; i <= 5; i++) {
            String date = dateMap.get(i).get(0);
            outcomeList0.add(outcomeList0tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList1.add(outcomeList1tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList2.add(outcomeList2tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
            outcomeList3.add(outcomeList3tmp.stream().filter(p -> p.getDate().equals(date)).findFirst().orElse(null));
        }

        model.addAttribute("outcomeList0", outcomeList0);
        model.addAttribute("outcomeList1", outcomeList1);
        model.addAttribute("outcomeList2", outcomeList2);
        model.addAttribute("outcomeList3", outcomeList3);
        model.addAttribute("doctor", doctor);
//        model.addAttribute("doctors", doctors);
        return "outcome::docData";
    }

    @PostMapping("/outcome/save")
    @ResponseBody
    public Map<String, Object> outcomeSave(Model model, Outcome outcome) {
        Map<String, Object> res = new HashMap<>();
        Doctor doctor = doctorService.getById(outcome.getDocId());
        outcome.setClinicId(doctor.getClinicId());
        Outcome outcomeTmp = outcomeService.get(outcome);
        if (outcomeTmp != null) {
            outcomeService.updateState(outcome);
        } else {
            outcomeService.insert(outcome);
        }
        res.put("state", "success");
        return res;
    }

    @RequestMapping("/admin-statistics")
    public String statistics(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        model.addAttribute("user", user);
        List<Outcome> dates = outcomeService.getAllDate();
        model.addAttribute("dates", dates);
        List<Clinic> clinics = outcomeService.getAllClinic();
        model.addAttribute("clinics", clinics);
        return "statistics";
    }

    @PostMapping("/admin-statistics/getchart")
    @ResponseBody
    public Map<String, Object> statisticsGetChart(Model model, String date, String clinicId, HttpServletRequest request) {
        Map<String, Object> res = new HashMap<>();
        List<Outcome> outcomes = outcomeService.getOutcomeByDateAndClinic(date, clinicId);
        List<Appointment> appointments = appointmentService.getAppointmentByDateAndClinic(date, clinicId);
        int[] outcomeArr = new int[8];
        for (Outcome outcome : outcomes) {
            if (outcome.getStage() != null) {
                switch (outcome.getStage()) {
                    case "0":
                        outcomeArr[0] = outcome.getDocCount();
                        outcomeArr[1] = outcome.getDocCount();
                        outcomeArr[2] = outcome.getDocCount();
                        break;
                    case "1":
                        outcomeArr[3] = outcome.getDocCount();
                        outcomeArr[4] = outcome.getDocCount();
                        outcomeArr[5] = outcome.getDocCount();
                        break;
                    case "2":
                        outcomeArr[6] = outcome.getDocCount();
                        break;
                    case "3":
                        outcomeArr[7] = outcome.getDocCount();
                        break;
                }

            }
        }
        int[] appointmentArr = new int[7];
        for (Appointment appointment : appointments) {
            if (appointment.getPeriod() != null) {
                appointmentArr[appointment.getPeriod()] = appointment.getPatCount();
            }
        }
        int[] limitArr = new int[8];
        for (int i = 0; i < limitArr.length; i++) {
            limitArr[i] = outcomeArr[i] * 10;
        }
        res.put("outcomeArr", outcomeArr);
        res.put("appointmentArr", appointmentArr);
        res.put("limitArr", limitArr);
        res.put("state", "success");
        return res;
    }

}
